<style>
  .header-font {
    font-size: 15px;
    font-weight: bold;
  }
  .line {
    margin: 10px 0;
    width: 100%;
    border: 0.5px solid rgb(218, 218, 218);
  }
  .table {
    margin-top:10px;
  }
  .operate-form {
    margin-bottom: -10px;
  }
  .paging {
    float:right;
    margin-top:10px;
  }
  .btn-export {
    float: left;
  }
  .btn-upload {
    float: left;
  }
</style>
<template>
  <div>
    <div class='header-font'>
      <Icon type="md-apps" />
      <span>运营项目统计表</span>
    </div>
    <div class="line"></div>
    <operate-form
      @on-success-valid="handleSubmit"
      class="operate-form">
    </operate-form>
    <Button
      icon="md-add"
      type="primary"
      @click="handleClick">
      添加
    </Button>
    <!-- <btn-export :tableData="operateList" :tableTitle="columns" :title="title" class="btn-export"></btn-export> -->
    <!-- <btn-upload class="btn-upload" :title="title"></btn-upload> -->
    <operate-modal
      @operate-valid="handleEdit">
    </operate-modal>
    <Table
      :columns="columns"
      :data="historyData"
      border
      class="table">
    </Table>
    <table-page
      class="paging"
      :dataCount="dataCount"
      @change-page="changePage"
      :pageSize="pageSize">
    </table-page>
  </div>
</template>
<script>
import {
  mapActions,
  mapState,
  mapGetters
} from 'vuex'
import OperateForm from '_c/operate-form'
import OperateModal from '_c/operate-modal'
import TablePage from '_c/page'
import BtnExport from '_c/btn-export'
import BtnUpload from '_c/btn-upload'
export default {
  components: {
    OperateForm,
    OperateModal,
    TablePage,
    BtnExport,
    BtnUpload
  },
  data () {
    return {
      page: 1,
      title: '运营项目统计表',
      columns: [
        {
          title: '序号',
          type: 'index2',
          width: 60,
          align: 'center',
          render: (h, params) => {
            return h('span', params.index + (this.page - 1) * this.pageSize + 1)
          }
        },
        {
          title: '项目名称',
          key: 'name',
          align: 'center'
        },
        {
          title: '创建时间',
          key: 'ctime',
          align: 'center'
        },
        {
          title: '推广单价(元)',
          key: 'unitprice',
          align: 'center'
        },
        {
          title: '启用状态',
          key: 'state',
          align: 'center',
          render: (h, params) => {
            return h(
              'span',
              {},
              params.row.state === '0' ? '启用' : '禁用'
            )
          }
        },
        {
          title: '项目地址',
          key: 'url',
          align: 'center',
          render: (h, params) => {
            return h(
              'span',
              {},
              params.row.url ? params.row.url : ''
            )
          }
        }
      ]
    }
  },
  computed: {
    ...mapState({
      operateList: state => state.operate.operateList,
      dataCount: state => state.operate.operateDataCount,
      historyData: state => state.operate.operateHistoryData,
      pageSize: state => state.operate.pageSize
    })
  },
  methods: {
    ...mapActions([
      'getOperateList',
      'updateModalState',
      'getOperateCurrentRow',
      'editOperateData',
      'changeOperatePage'
    ]),
    handleSubmit (playload) {
      if (playload.proid !== '' || playload.ctime !== '') {
        this.getOperateList(playload)
      }
    },
    handleClick (id, row) {
      let newRow = {}
      this.id = id
      if (row) {
        newRow = Object.assign({}, row)
      }
      this.getOperateCurrentRow(newRow)
      this.updateModalState(true)
    },
    handleEdit (playload, id) {
      this.editOperateData(playload).then(
        setTimeout(() => {
          this.getOperateList({ page: this.page })
        }, 100)
      )
      this.updateModalState(false)
    },
    changePage (index) {
      this.page = index
      this.changeOperatePage(index)
      this.getOperateList({ page: index })
    }
  },
  created () {
    this.getOperateList({})
  }
}
</script>
